{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 10 数据连接",
   "id": "1ef6a48443beb4bf"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T15:25:48.061039Z",
     "start_time": "2025-01-08T15:25:48.053256Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "df_obj1 = pd.DataFrame({'key': ['b', 'b', 'a', 'c', 'a', 'a', 'b'],\n",
    "                        'data1': np.random.randint(0, 10, 7)})\n",
    "df_obj2 = pd.DataFrame({'key': ['a', 'b', 'd'],\n",
    "                        'data2': np.random.randint(0, 10, 3)})\n",
    "\n",
    "print(df_obj1)\n",
    "print(\"-\"*50)\n",
    "print(df_obj2)"
   ],
   "id": "4e6f9380e0727608",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  key  data1\n",
      "0   b      9\n",
      "1   b      9\n",
      "2   a      6\n",
      "3   c      4\n",
      "4   a      8\n",
      "5   a      5\n",
      "6   b      1\n",
      "--------------------------------------------------\n",
      "  key  data2\n",
      "0   a      7\n",
      "1   b      5\n",
      "2   d      6\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T15:25:50.604877Z",
     "start_time": "2025-01-08T15:25:50.594929Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#默认连接使用相同的列名，连接方式是内连接\n",
    "print(type(pd.merge(df_obj1, df_obj2))) # DataFrame \n",
    "pd.merge(df_obj1, df_obj2)"
   ],
   "id": "7c458d3d9b91a5d7",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "  key  data1  data2\n",
       "0   b      9      5\n",
       "1   b      9      5\n",
       "2   a      6      7\n",
       "3   a      8      7\n",
       "4   a      5      7\n",
       "5   b      1      5"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>key</th>\n",
       "      <th>data1</th>\n",
       "      <th>data2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>b</td>\n",
       "      <td>9</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>b</td>\n",
       "      <td>9</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>a</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>a</td>\n",
       "      <td>8</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>a</td>\n",
       "      <td>5</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>b</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T15:26:42.190317Z",
     "start_time": "2025-01-08T15:26:42.181334Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# ?? \n",
    "#左df和右df都拿索引连接\n",
    "pd.merge(df_obj1, df_obj2,left_index=True,right_index=True)"
   ],
   "id": "f6a05d5b81df439f",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "  key_x  data1 key_y  data2\n",
       "0     b      9     a      7\n",
       "1     b      9     b      5\n",
       "2     a      6     d      6"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>key_x</th>\n",
       "      <th>data1</th>\n",
       "      <th>key_y</th>\n",
       "      <th>data2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>b</td>\n",
       "      <td>9</td>\n",
       "      <td>a</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>b</td>\n",
       "      <td>9</td>\n",
       "      <td>b</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>a</td>\n",
       "      <td>6</td>\n",
       "      <td>d</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T15:28:32.164135Z",
     "start_time": "2025-01-08T15:28:32.156035Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 左表和右表都拿key列来连接\n",
    "pd.merge(df_obj1, df_obj2,on='key')"
   ],
   "id": "e8a7e9849193c3b6",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "  key  data1  data2\n",
       "0   b      9      5\n",
       "1   b      9      5\n",
       "2   a      6      7\n",
       "3   a      8      7\n",
       "4   a      5      7\n",
       "5   b      1      5"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>key</th>\n",
       "      <th>data1</th>\n",
       "      <th>data2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>b</td>\n",
       "      <td>9</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>b</td>\n",
       "      <td>9</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>a</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>a</td>\n",
       "      <td>8</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>a</td>\n",
       "      <td>5</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>b</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T15:30:12.074768Z",
     "start_time": "2025-01-08T15:30:12.067889Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 更改列名\n",
    "df_obj1 = df_obj1.rename(columns={'key':'key1'})\n",
    "df_obj2 = df_obj2.rename(columns={'key':'key2'})\n",
    "\n",
    "print(df_obj1)\n",
    "print(\"-\"*50)\n",
    "print(df_obj2)"
   ],
   "id": "241dd3f81983536a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  key1  data1\n",
      "0    b      9\n",
      "1    b      9\n",
      "2    a      6\n",
      "3    c      4\n",
      "4    a      8\n",
      "5    a      5\n",
      "6    b      1\n",
      "--------------------------------------------------\n",
      "  key2  data2\n",
      "0    a      7\n",
      "1    b      5\n",
      "2    d      6\n"
     ]
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T15:30:51.472288Z",
     "start_time": "2025-01-08T15:30:51.463459Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#左表以key1来连接，右表以key2来连接\n",
    "pd.merge(df_obj1, df_obj2, left_on='key1', right_on='key2')"
   ],
   "id": "916325b124570fa8",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "  key1  data1 key2  data2\n",
       "0    b      9    b      5\n",
       "1    b      9    b      5\n",
       "2    a      6    a      7\n",
       "3    a      8    a      7\n",
       "4    a      5    a      7\n",
       "5    b      1    b      5"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>key1</th>\n",
       "      <th>data1</th>\n",
       "      <th>key2</th>\n",
       "      <th>data2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>b</td>\n",
       "      <td>9</td>\n",
       "      <td>b</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>b</td>\n",
       "      <td>9</td>\n",
       "      <td>b</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>a</td>\n",
       "      <td>6</td>\n",
       "      <td>a</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>a</td>\n",
       "      <td>8</td>\n",
       "      <td>a</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>a</td>\n",
       "      <td>5</td>\n",
       "      <td>a</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>b</td>\n",
       "      <td>1</td>\n",
       "      <td>b</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T15:36:21.935615Z",
     "start_time": "2025-01-08T15:36:21.924605Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#全外连接\n",
    "pd.merge(df_obj1, df_obj2, left_on='key1', right_on='key2', how='outer') "
   ],
   "id": "e222bee205b85116",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "  key1  data1 key2  data2\n",
       "0    a    6.0    a    7.0\n",
       "1    a    8.0    a    7.0\n",
       "2    a    5.0    a    7.0\n",
       "3    b    9.0    b    5.0\n",
       "4    b    9.0    b    5.0\n",
       "5    b    1.0    b    5.0\n",
       "6    c    4.0  NaN    NaN\n",
       "7  NaN    NaN    d    6.0"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>key1</th>\n",
       "      <th>data1</th>\n",
       "      <th>key2</th>\n",
       "      <th>data2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>a</td>\n",
       "      <td>6.0</td>\n",
       "      <td>a</td>\n",
       "      <td>7.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>a</td>\n",
       "      <td>8.0</td>\n",
       "      <td>a</td>\n",
       "      <td>7.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>a</td>\n",
       "      <td>5.0</td>\n",
       "      <td>a</td>\n",
       "      <td>7.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>b</td>\n",
       "      <td>9.0</td>\n",
       "      <td>b</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>b</td>\n",
       "      <td>9.0</td>\n",
       "      <td>b</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>b</td>\n",
       "      <td>1.0</td>\n",
       "      <td>b</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>c</td>\n",
       "      <td>4.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>d</td>\n",
       "      <td>6.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T15:36:54.282350Z",
     "start_time": "2025-01-08T15:36:54.273322Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#left join 等价于 left  join\n",
    "pd.merge(df_obj1, df_obj2, left_on='key1', right_on='key2', how='left')"
   ],
   "id": "6f5c30b60cb20677",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "  key1  data1 key2  data2\n",
       "0    b      9    b    5.0\n",
       "1    b      9    b    5.0\n",
       "2    a      6    a    7.0\n",
       "3    c      4  NaN    NaN\n",
       "4    a      8    a    7.0\n",
       "5    a      5    a    7.0\n",
       "6    b      1    b    5.0"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>key1</th>\n",
       "      <th>data1</th>\n",
       "      <th>key2</th>\n",
       "      <th>data2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>b</td>\n",
       "      <td>9</td>\n",
       "      <td>b</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>b</td>\n",
       "      <td>9</td>\n",
       "      <td>b</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>a</td>\n",
       "      <td>6</td>\n",
       "      <td>a</td>\n",
       "      <td>7.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>c</td>\n",
       "      <td>4</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>a</td>\n",
       "      <td>8</td>\n",
       "      <td>a</td>\n",
       "      <td>7.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>a</td>\n",
       "      <td>5</td>\n",
       "      <td>a</td>\n",
       "      <td>7.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>b</td>\n",
       "      <td>1</td>\n",
       "      <td>b</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T15:37:08.607307Z",
     "start_time": "2025-01-08T15:37:08.598232Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# right等价于数据库的right join\n",
    "pd.merge(df_obj1, df_obj2, left_on='key1', right_on='key2', how='right')"
   ],
   "id": "ab8d616c3cad29d2",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "  key1  data1 key2  data2\n",
       "0    a    6.0    a      7\n",
       "1    a    8.0    a      7\n",
       "2    a    5.0    a      7\n",
       "3    b    9.0    b      5\n",
       "4    b    9.0    b      5\n",
       "5    b    1.0    b      5\n",
       "6  NaN    NaN    d      6"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>key1</th>\n",
       "      <th>data1</th>\n",
       "      <th>key2</th>\n",
       "      <th>data2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>a</td>\n",
       "      <td>6.0</td>\n",
       "      <td>a</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>a</td>\n",
       "      <td>8.0</td>\n",
       "      <td>a</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>a</td>\n",
       "      <td>5.0</td>\n",
       "      <td>a</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>b</td>\n",
       "      <td>9.0</td>\n",
       "      <td>b</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>b</td>\n",
       "      <td>9.0</td>\n",
       "      <td>b</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>b</td>\n",
       "      <td>1.0</td>\n",
       "      <td>b</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>d</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T15:38:23.863677Z",
     "start_time": "2025-01-08T15:38:23.854180Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 处理重复列名\n",
    "df_obj1 = pd.DataFrame({'key': ['b', 'b', 'a', 'c', 'a', 'a', 'b'],\n",
    "                        'data' : np.random.randint(0,10,7)})\n",
    "df_obj2 = pd.DataFrame({'key': ['a', 'b', 'd'],\n",
    "                        'data' : np.random.randint(0,10,3)})\n",
    "print(df_obj1)\n",
    "print(\"-\"*50)\n",
    "print(df_obj2)\n",
    "print(\"-\"*50)\n",
    "\n",
    "#给相同的数据列添加后缀\n",
    "print(pd.merge(df_obj1, df_obj2, on='key', suffixes=('_left', '_right')))"
   ],
   "id": "62b902caca3c6865",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  key  data\n",
      "0   b     0\n",
      "1   b     7\n",
      "2   a     7\n",
      "3   c     8\n",
      "4   a     1\n",
      "5   a     9\n",
      "6   b     7\n",
      "--------------------------------------------------\n",
      "  key  data\n",
      "0   a     3\n",
      "1   b     9\n",
      "2   d     0\n",
      "--------------------------------------------------\n",
      "  key  data_left  data_right\n",
      "0   b          0           9\n",
      "1   b          7           9\n",
      "2   a          7           3\n",
      "3   a          1           3\n",
      "4   a          9           3\n",
      "5   b          7           9\n"
     ]
    }
   ],
   "execution_count": 15
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T15:39:45.620468Z",
     "start_time": "2025-01-08T15:39:45.613456Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 按索引连接\n",
    "df_obj1 = pd.DataFrame({'key': ['b', 'b', 'a', 'c', 'a', 'a', 'b'],\n",
    "                        'data1' : np.random.randint(0,10,7)})\n",
    "df_obj2 = pd.DataFrame({'data2' : np.random.randint(0,10,3)}, index=['a', 'b', 'd'])\n",
    "print(df_obj1)\n",
    "print(df_obj2)"
   ],
   "id": "a55d847f409276ec",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  key  data1\n",
      "0   b      5\n",
      "1   b      6\n",
      "2   a      2\n",
      "3   c      5\n",
      "4   a      4\n",
      "5   a      7\n",
      "6   b      9\n",
      "   data2\n",
      "a      9\n",
      "b      3\n",
      "d      4\n"
     ]
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T15:39:55.964381Z",
     "start_time": "2025-01-08T15:39:55.957857Z"
    }
   },
   "cell_type": "code",
   "source": "print(pd.merge(df_obj1, df_obj2, left_on='key', right_index=True))",
   "id": "485eaace9e664b63",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  key  data1  data2\n",
      "0   b      5      3\n",
      "1   b      6      3\n",
      "2   a      2      9\n",
      "4   a      4      9\n",
      "5   a      7      9\n",
      "6   b      9      3\n"
     ]
    }
   ],
   "execution_count": 17
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T15:40:26.978299Z",
     "start_time": "2025-01-08T15:40:26.971778Z"
    }
   },
   "cell_type": "code",
   "source": "print(pd.merge(df_obj2,df_obj1, left_index=True, right_on='key'))",
   "id": "9165ea53f3573bcb",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   data2 key  data1\n",
      "2      9   a      2\n",
      "4      9   a      4\n",
      "5      9   a      7\n",
      "0      3   b      5\n",
      "1      3   b      6\n",
      "6      3   b      9\n"
     ]
    }
   ],
   "execution_count": 18
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
